//
//  QDScreenShotAlertView.m
//  QDMetroClient
//
//  Created by YueWen on 2019/1/2.
//  Copyright © 2019 YueWen. All rights reserved.
//

#import "QDScreenShotAlertView.h"

@implementation QDScreenShotAlertView

@synthesize delegate = _delegate;


+ (instancetype)sharedInstance
{
    static id shareInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        shareInstance = [self new];
    });
    
    return shareInstance;
}


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.title = @"地铁乘车码实时刷新，截图中的二维码无法使用哦~";
        
        self.containerView = ({
            
            UIView *view = [UIView new];
            view.backgroundColor = UIColor.whiteColor;
            view.layer.cornerRadius = 4;
            view.clipsToBounds = true;
            
            view;
        });
        
        self.titleLabel = ({
            
            UILabel *label = [UILabel new];
            label.backgroundColor = [UIColor whiteColor];
            label.text = @"截图提醒";
            label.font = RITLRegularFont(16);
            label.textColor = QDSColorDefaultSeparator;
            
            label;
        });
        
        self.textLabel = ({
            
            UILabel *label = [UILabel new];
            label.backgroundColor = [UIColor whiteColor];
            label.numberOfLines = 0;
            label.textAlignment = NSTextAlignmentCenter;
            label.text = @"地铁乘车码实时刷新，截图中的二维码无法使用哦~";
            label.font = RITLRegularFont(14);
            label.textColor = RITLColorFromIntRBG(88, 89, 89);
            
            label;
        });
        
        self.tureButton = ({
            
            UIButton *view = [UIButton new];
            view.adjustsImageWhenHighlighted = false;
            view.backgroundColor = [UIColor whiteColor];
            view.titleLabel.font = RITLRegularFont(16);
            [view setTitle:@"我知道了" forState:UIControlStateNormal];
            [view setTitleColor:RITLColorFromIntRBG(105, 176, 119) forState:UIControlStateNormal];
            
            view;
        });
        [self.tureButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        
        [self.contentView addSubview:self.containerView];
        [self.containerView addSubview:self.titleLabel];
        [self.containerView addSubview:self.textLabel];
        [self.containerView addSubview:self.tureButton];
        
        
        //获得垂直位置的起点
        CGFloat spaceHeight = UIScreen.mainScreen.ritl_height - 160;
        CGFloat topScale = 215.0 / (215 + 292);
        CGFloat contrainerWidth = self.ritl_width - NWScale(55 * 2);
        
        
        //进行固定设置
        self.containerView.ritl_size = CGSizeMake(contrainerWidth, 160);
        self.containerView.ritl_centerX = self.ritl_width / 2.0;
        self.containerView.ritl_originY = spaceHeight * topScale;
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.offset(0);
            make.top.offset(16);
            make.height.mas_lessThanOrEqualTo(22);
        }];
        
        [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.offset(0);
            make.left.offset(32);
            make.right.inset(32);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(15);
        }];
        
        [self.tureButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.offset(0);
            make.height.mas_equalTo(52);
        }];
        
        
        /// 横线
        UIView *lineView = ({
            
            UIView *view = [UIView new];
            view.backgroundColor = RITLColorFromIntRBG(227, 230, 228);
            
            view;
        });
        
        [self.containerView addSubview:lineView];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.inset(52);
            make.left.right.offset(0);
            make.height.mas_equalTo(0.5);
        }];
    }
    
    return self;
}


+ (void)showAndBecomeDelegate:(id<QDScreenShotAlertViewDelegate>)delegate title:(NSString *)title
{
    QDScreenShotAlertView.sharedInstance.delegate = delegate;
    if(title == nil){
        QDScreenShotAlertView.sharedInstance.title = title;
    }
    
    [self show];
}



- (void)showAnimated {
    self.containerView.hidden = false;
}



- (void)dismissAnimated {
    self.containerView.hidden = true;
    QDScreenShotAlertView.sharedInstance.delegate = nil;
    [self removeFromSuperview];
}



- (void)setTitle:(NSString *)title {
    _title = title;
    self.titleLabel.text = title;
}

@end
